String.prototype.getAns = function() {
  var pattern = /(?=((?!\b)\d{3})+$)/g;
  return this.replace(pattern, ',');
}
function changeDataType(obj){
  let str = ''
  if(typeof obj == 'object'){
    for(let i in obj){
      if(typeof obj[i] != 'function' && typeof obj[i] != 'object' && typeof obj[i]!="undefined"){
        str += i + '=' + obj[i] + '&' ;
      }else if (typeof obj[i] == 'object'){
        str += changeSonType(i, obj[i])
      }
    }
  }
  return str.replace(/&$/g, '');
}
function changeSonType(objName, objValue){
  let nextStr = "";
  if(typeof objValue == 'object'){
    for(let i in objValue){
      if(typeof objValue[i] != 'object'){
        let value = objName + '[' + i + ']=' + objValue[i];
        nextStr += encodeURI(value) + '&';
      }else{
        changeSonType(objName + '[' + i + ']', objValue[i]);
      }
    }
  }
  return nextStr;
}

window.confirm = function (vm,message, fn) {
  vm.$confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(()=>fn&&fn()).catch(() => {
    vm.$message({
      type: 'info',
      message: '已取消'
    });
  });
};
window.msg = function (vm,tip) {
  vm.$message({
    type: 'success',
    message: tip||'操作成功!'
  });
};


function addInterceptor(Vue,axios){
  Vue.prototype.$http = axios;
  axios.interceptors.response.use(response => {
    /*return {
      code: response.status,
      message: response.statusText,
      data: response.data
    }*/
    return response.data
  }, err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '错误请求'
          break
        case 401:
          err.message = '未授权，请重新登录'
          break
        case 403:
          err.message = '拒绝访问'
          break
        case 404:
          err.message = '请求错误,未找到该资源'
          break
        case 405:
          err.message = '请求方法未允许'
          break
        case 408:
          err.message = '请求超时'
          break
        case 500:
          err.message = '服务器端出错'
          break
        case 501:
          err.message = '网络未实现'
          break
        case 502:
          err.message = '网络错误'
          break
        case 503:
          err.message = '服务不可用'
          break
        case 504:
          err.message = '网络超时'
          break
        case 505:
          err.message = 'http版本不支持该请求'
          break
        default:
          err.message = `连接错误${err.response.status}`
      }
      let errData = {
        code: err.response.status,
        message: err.message
      }
      // 统一错误处理可以放这，例如页面提示错误...
      console.log('统一错误处理: ', errData)
    }
    return Promise.reject(err)
  })
}

function formatNum(value,digitNum){
    if(!value){
      return 0;
    }
    if(!digitNum||digitNum == 0){
      var pattern = /(?=((?!\b)\d{3})+$)/g;
      return value.toString().replace(pattern, ',');
    }
    if(!value) return '0.00';
    var intPart = Number(value).toFixed(0); //获取整数部分
    var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断
    var floatPart = ".00"; //预定义小数部分
    var value2Array = value.split(".");
    //=2表示数据有小数位
    if(value2Array.length == 2) {
      floatPart = value2Array[1].toString(); //拿到小数部分
      if(floatPart.length == 1) { //补0,实际上用不着
        return intPartFormat + "." + floatPart + '0';
      } else {
        return intPartFormat + "." + floatPart;
      }
    } else {
      return intPartFormat + floatPart;
    }
}

function init(Vue,axios){
  window.changeDataType = changeDataType;
  addInterceptor(Vue,axios);
  Vue.filter('formatNum', formatNum)
}





export default {
  name:"util",
  changeDataType:changeDataType,
  changeSonType:changeSonType,
  addInterceptor:addInterceptor,
  init:init
}
